package com.skyline.energy.datasource;

import java.util.List;

import javax.sql.DataSource;

import com.skyline.energy.dataaccess.jdbc.ShardParam;
import com.skyline.energy.dataaccess.jdbc.SqlExecuteContext;
import com.skyline.energy.dataaccess.jdbc.SqlExecuteContextHolder;

public class ShardDataSource extends DistributeDataSource {
	List<List<NodeDataSource>> nodes;

	@Override
	public DataSource getDataSource() {
		DataSource txDataSource = getTxDataSource();
		if (txDataSource == null) {
			// TODO
			return null;
		}

		if (canUseOld(txDataSource)) {
			return txDataSource;
		} else {
			// TODO
			return null;
		}
	}

	private boolean canUseOld(DataSource txDataSource) {
		SqlExecuteContext sqlContext = SqlExecuteContextHolder.getContext();
		NodeDataSource nodeDataSource = (NodeDataSource) txDataSource;
		if(nodeDataSource.getShard() == caculateShard(sqlContext.getShardParam())) {
			return true;
		}
		return false;
	}

	private int caculateShard(ShardParam shardParam) {
		return 0;
	}
}
